package org.lsy.servlet;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.lsy.component.ByeConsumer;
import org.lsy.component.HelloConsumer;
import org.lsy.utils.ContextUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liusy95
 * @version 1.0
 * @date 2020/7/1
 */
@WebServlet(name="myServlet",urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {

    private HelloConsumer helloConsumer;

    private ByeConsumer byeConsumer;

    @Override
    public void init() throws ServletException {
        AnnotationConfigApplicationContext context = ContextUtils.getInstance();
        helloConsumer = context.getBean(HelloConsumer.class);
        byeConsumer = context.getBean(ByeConsumer.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 设置请求体的字符编码
        req.setCharacterEncoding("UTF-8");
        String info = helloConsumer.hello("test") + "    " + byeConsumer.bye("test");
        // 响应
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().print(info);
    }
}
